package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 吴
 * @version V1.0
 * @Description  预约管理
 * @Package com.itheima.controller
 * @date 2021/11/24 16:29
 */
@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 显示当月的可预约的信息
     * @param date 当前年和月
     * @return
     */
    @GetMapping("/getOrderSettingsByMonth")
    public Result getOrderSettingsByMonth(@DateTimeFormat(pattern = "yyyy-MM") Date date){
        try {
            //调用service,查询结果
            List<Map<String,Object>> orderSettings=orderSettingService.getOrderSettingsByMonth(date);

            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettings);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }


    /**
     * 批量保存信息
     * @param excelFile  excel 文件
     * @return
     */
    @PostMapping("/batchSave")
    public Result batchSave(MultipartFile excelFile){
        try {
            //1 调用POIUtils的readExcel从excelFile中读取内容,结果List<String[]>
            List<String[]> excels = POIUtils.readExcel(excelFile);
            //2 将List<String[]>集合转换成List<OrderSetting>集合
            List<OrderSetting> list = excels.stream().map(arr -> {
                //将日期转换成Date类型
                Date orderDate = null;
                try {
                    orderDate = new SimpleDateFormat("yyyy/MM/dd").parse(arr[0]);
                } catch (ParseException  e) {
                    e.printStackTrace();
                    throw new RuntimeException("解析日期失败!");
                }
                //创建OrderSetting对象并设置参数
                return new OrderSetting(orderDate, Integer.parseInt(arr[1]));
            }).collect(Collectors.toList());


            //3 调用service批量导入预约设置
            orderSettingService.batchSaveOrderSetting(list);

            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 保存修改单条信息
     * @param orderSetting  预约信息
     * @return
     */
    @PostMapping("/saveSingle")
    public Result saveSingle(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.saveOrderSetting(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }



}
